﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Peekaboo2008.model;
using Peekaboo2008.dao;
using Peekaboo2008.common;

namespace Peekaboo2008.view
{
    public partial class FormChiTietNhaCungCap : Form
    {
        private FormNhaCungCap formNCC;
        private KhachHangModel nccModel = null;
        private FormNhapHangTuNCC formNhapHangTuNCC;

        public FormChiTietNhaCungCap(ref FormNhapHangTuNCC _FormNhapHangTuNCC)
        {
            InitializeComponent();
            FormChiTietNhaCungCapLoad();
            formNhapHangTuNCC = _FormNhapHangTuNCC;

        }
        public FormChiTietNhaCungCap(ref FormNhaCungCap _formNCC)
        {
            InitializeComponent();
            FormChiTietNhaCungCapLoad();
            formNCC = _formNCC;
            
        }

        public FormChiTietNhaCungCap(KhachHangModel _nccModel, ref FormNhaCungCap _formNCC)
        {
            InitializeComponent();
            FormChiTietNhaCungCapLoad();
            formNCC = _formNCC;
            nccModel = _nccModel;
            txtHoTen.Text = nccModel.Ten;
            txtDiaChi.Text = nccModel.DiaChi;
            txtDienThoai.Text = nccModel.Tel;
            txtEmail.Text = nccModel.Email;
            txtFax.Text = nccModel.Fax;
            txtGhiChu.Text = nccModel.GhiChu;
            txtHoTen.Text = nccModel.Ten;
            txtMaSoThue.Text = nccModel.MaSoThue;
            txtTaiKhoan.Text = nccModel.SoTaiKhoan;
            txtNganHang.Text = nccModel.NganHang;
            cbbKhuVuc.SelectedValue = nccModel.MaKhuVuc;
            cbbNhom.SelectedValue = nccModel.MaNhomKhachHang;
            
        }

        private void FormChiTietNhaCungCap_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormChiTietNhaCungCap_KeyDown);
        }

        public void FormChiTietNhaCungCapLoad()
        {
            DataTable dtKhuVuc = KhuVucDAO.getAllOrderByName();
            cbbKhuVuc.DataSource = dtKhuVuc;
            cbbKhuVuc.ValueMember = "ID";
            cbbKhuVuc.DisplayMember = "Ten";

            DataTable dtNhom = NhomKhachHangDAO.getAllNCCOrderByName();
            cbbNhom.DataSource = dtNhom;
            cbbNhom.ValueMember = "ID";
            cbbNhom.DisplayMember = "Ten";
        }

        private void FormChiTietNhaCungCap_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F8)
            {
                btnChapNhan.PerformClick();
            }
            if (e.KeyCode == Keys.F12)
            {
                this.Dispose();
            }
            if (e.KeyCode == Keys.F6)
            {
                btThemKhuVuc.PerformClick();
            }
            if (e.KeyCode == Keys.F7)
            {
                btThemNhom.PerformClick();
            }
        }

        private void btnChapNhan_Click(object sender, EventArgs e)
        {
            if (txtHoTen.Text.Equals(""))
            {
                MessageBox.Show("Họ Tên Nhà Cung Cấp Không Được Trống");
                txtHoTen.Focus();
            }
            else
            {
                if (nccModel == null)
                {
                    nccModel = new KhachHangModel();
                }
                nccModel.DiaChi = txtDiaChi.Text;
                nccModel.Email = txtEmail.Text;
                nccModel.Fax = txtFax.Text;
                nccModel.GhiChu = txtGhiChu.Text;
                nccModel.MaKhuVuc = cbbKhuVuc.SelectedValue.ToString();
                nccModel.MaNhomKhachHang = cbbNhom.SelectedValue.ToString();
                nccModel.MaSoThue = txtMaSoThue.Text;
                nccModel.SoTaiKhoan = txtTaiKhoan.Text;
                nccModel.NganHang = txtNganHang.Text;
                nccModel.Tel = txtDienThoai.Text;
                nccModel.Ten = txtHoTen.Text;
                // new
                if (nccModel.Id == null || nccModel.Id.ToString() == "")
                {
                    if (KhachHangDAO.Insert(nccModel))
                    {
                        MessageBox.Show("Thêm nhà cung cấp thành công");
                        if (formNCC != null)
                        {
                            formNCC.FormNhaCungCapLoad();
                        }
                        if (formNhapHangTuNCC != null)
                        {
                            String _MaKhuVuc = ChiNhanhDAO.getByMaChiNhanh(Constants.USER_LOGIN.MaChiNhanh).MaKhuVuc;
                            DataTable _DanhSachNhaCungCapKhuVuc = KhachHangDAO.NhaCungCap_GetByMaKhuVuc(_MaKhuVuc);
                            formNhapHangTuNCC.initKhachHang(_DanhSachNhaCungCapKhuVuc);
                        }
                        this.Dispose();
                    }
                }
                //update
                else
                {
                    if (KhachHangDAO.Update(nccModel))
                    {
                        MessageBox.Show("Cập nhật nhà cung cấp thành công");
                        formNCC.FormNhaCungCapLoad();
                        this.Dispose();
                    }
                }
            }
        }

        private void btThemKhuVuc_Click(object sender, EventArgs e)
        {
            FormChiTietNhaCungCap formChiTietNhaCungCap = this;
            FormChiTietKhuVuc formChiTietKhuVuc = new FormChiTietKhuVuc(ref formChiTietNhaCungCap);
            formChiTietKhuVuc.ShowDialog();
        }

        private void btThemNhom_Click(object sender, EventArgs e)
        {
            FormChiTietNhaCungCap formChiTietNhaCungCap = this;
            FormChiTietNhomNCC formChiTietNCC = new FormChiTietNhomNCC(ref formChiTietNhaCungCap);
            formChiTietNCC.ShowDialog();
        }

        private void btnKetThuc_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
